package com.leyou.item.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author CHERRY
 * @date 2018-09-01-19:50
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //添加CORS的配置信息
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许的域 不要写* 否则cookie无法使用
        corsConfiguration.addAllowedOrigin("http://manage.leyou.com");
        corsConfiguration.addAllowedOrigin("http://www.leyou.com");
        //是否发送cookie
        corsConfiguration.setAllowCredentials(true);
        //允许请求的方法
        corsConfiguration.addAllowedMethod("*");
        /*corsConfiguration.addAllowedMethod("GET");
        corsConfiguration.addAllowedMethod("PUT");
        corsConfiguration.addAllowedMethod("DELETE");
        corsConfiguration.addAllowedMethod("POST");
        corsConfiguration.addAllowedMethod("OPTIONS");
        corsConfiguration.addAllowedMethod("PATCH");*/
        //允许头的信息
        corsConfiguration.addAllowedHeader("*");
        //添加映射路径 拦截一切请求
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(configurationSource);
    }
}
